R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

system('inkscape --version', intern = TRUE)
## [1] "Inkscape 1.1.1 (3bf5ae0d25, 2021-09-20)"

Including Plots

You can also embed plots, for example:

is_url <- function(path){
  grepl("^https?://", path)
}

download_svg <- function(url){
  filename = tempfile("inx", fileext = ".svg")
  download.file(url, filename)
  return(filename)
}

Function to run an Inkscape extension in Windows

inx_extension <- function(input, extension, ext){
  path = system('inkscape --system-data-directory', intern = TRUE)
  inkscape_extensions_path = paste(path, "\\extensions", sep = "")
  inkscape_python_home  = paste(gsub("\\share\\inkscape", "", path, fixed = T), "\\bin", sep = "")
  if(is_url(input)) {
    input_file_path = download_svg(input)
  } else {
    input_file_path = tempfile("inx")
    file.copy(input, input_file_path)
  }
  output = tempfile("inx", fileext = ext)
  command = tempfile(pattern = "inx_", fileext = ".bat")
  '@ECHO OFF
cd %s
python.exe "%s" --output="%s"  "%s"' %>% sprintf(
  inkscape_python_home,
  paste(inkscape_extensions_path, extension, sep = "\\"),
  output,
  input_file_path) %>% writeLines(command)
  system(command)
  output
}

and the Linux equivalent

inx_extension <- function(input, extension, ext){
  path = system('inkscape --system-data-directory', intern = TRUE)
  inkscape_extensions_path = paste(path, "/extensions", sep = "")
  if(is_url(input)) {
    input_file_path = download_svg(input)
  } else {
    input_file_path = tempfile("inx")
    file.copy(input, input_file_path)
  }
  output <- tempfile("inx", fileext = ext)
  command <- sprintf('python %s --output="%s" "%s"', paste(inkscape_extensions_path, extension, sep = "/"), output, input_file_path)
  system(command, intern = TRUE)
  output
}
url <- 'https://upload.wikimedia.org/wikipedia/commons/1/1b/Red_Bird.svg'
logo <- inx_extension(input = url, extension = "dxf12_outlines.py", ext =".dxf") %>%
  st_read()
## Reading layer `entities' from data source 
##   `C:\Users\Jacek Pardyak\AppData\Local\Temp\RtmpEB1lPr\inx5f14283e2f43.dxf' 
##   using driver `DXF'
## Simple feature collection with 8654 features and 6 fields
## Geometry type: LINESTRING
## Dimension:     XY
## Bounding box:  xmin: 238.9009 ymin: -837.4039 xmax: 1105.521 ymax: 40.07607
## CRS:           NA
logo %>% ggplot() +
  geom_sf()

Polygonize

img <- url %>% inx_extension(extension = "dxf12_outlines.py", ext =".dxf") %>%
  st_read() %>%
  select(geometry) %>% st_union() %>% st_polygonize() %>% 
  first() 
## Reading layer `entities' from data source 
##   `C:\Users\Jacek Pardyak\AppData\Local\Temp\RtmpEB1lPr\inx5f145ed4625f.dxf' 
##   using driver `DXF'
## Simple feature collection with 8654 features and 6 fields
## Geometry type: LINESTRING
## Dimension:     XY
## Bounding box:  xmin: 238.9009 ymin: -837.4039 xmax: 1105.521 ymax: 40.07607
## CRS:           NA
result <- st_sfc() %>% st_sf(geometry = .)

for(i in c(1: length(img))) {
  tmp <- img %>% 
  nth(i) %>%
  st_sfc()  %>% st_sf(geometry = .) %>% mutate(facet = i)
  result <- tmp %>% bind_rows(result) 
}


result %>% ggplot() +
  geom_sf() +
  geom_sf_label(aes(label = facet)) +
  theme_void()

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
plot_ly(result,  split = ~facet, color = ~facet)
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
result <- result %>% filter(!facet %in% c(5, 56:60))
result %>% ggplot() +
  geom_sf() +
  theme_void()